using Godot;
using System;

public partial class Camera2d : Camera2D
{
	Play play;
	Vector2 ditudaxiao;
    public Vector2 GetScreenCenterPosition
	{
		get
		{
			var viewportSize = GetViewportRect().Size;
			var screenCenter = GlobalPosition + (viewportSize / 4) ;
			return screenCenter;
		}
    }
	
		// Called when the node enters the scene tree for the first time.
    public override void _Ready()
	{
		play = GetParent().GetNode<Play>("Play");
	    var ditu = GetParent().GetNode<TileMapLayer>("TileMapLayer");//获取地图节点
        ditudaxiao = ditu.GetLayerPixelSize();//通过GetLayerPixelSize()获取地图大小

    }

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		
		if(play.Position.X>GetScreenCenterPosition.X&&(play.Position.X< /*GetViewportRect().Size.X * 0.75f*2)*/ditudaxiao.X- (GetViewportRect().Size.X / 4)))//需要找到地图的大小 才能限制摄像机的移动范围 这里的大小是随便写的 需要按照实际地图大小来
        {
			
			GlobalPosition = new Vector2(play.Position.X - GetViewportRect().Size.X / 4, GlobalPosition.Y);
        }
		if(play.Position.X< GetScreenCenterPosition.X && play.Position.X > GetViewportRect().Size.X/4)
		{
			
			GlobalPosition = new Vector2(play.Position.X - GetViewportRect().Size.X / 4, GlobalPosition.Y);
        }
        if (play.Position.Y > GetScreenCenterPosition.Y && (play.Position.Y < ditudaxiao.Y - (GetViewportRect().Size.Y / 4)))
		{
			GlobalPosition = new Vector2(GlobalPosition.X, play.Position.Y - GetViewportRect().Size.Y / 4);
        }
		if(play.Position.Y<GetScreenCenterPosition.Y && play.Position.Y > (GetViewportRect().Size.Y /4))
		{
            GlobalPosition = new Vector2(GlobalPosition.X, play.Position.Y - GetViewportRect().Size.Y / 4);
        }

	}
}
